/*
 * @Descripttion:
 * @Author: voanit
 * @Date: 2022-03-22 09:40:37
 * @LastEditors: voanit
 * @LastEditTime: 2022-03-22 12:11:16
 */
const initState = [
  { id: 1, name: '吃饭', isDone: false },
  { id: 2, name: '睡觉', isDone: true },
]
function todo(state = initState, action) {
  console.log(action, 88)
  switch (action.type) {
    case 'todo/add':
      return [
        ...state,
        { id: state.length + 1, name: action.payload, isDone: false },
      ]
    case 'todo/del':
      return state.filter((item) => item.id !== action.payload)
    case 'todo/changUpdate':
      return state.map((item) => {
        if (item.id === action.payload.id) {
          return {
            ...item,
            isDone: action.payload.isDone,
          }
        } else {
          return item
        }
      })
    case 'todo/changeAll':
      return state.map((it) => ({ ...it, isDone: action.payload }))
    case 'todo/update':
      return state.map((item) => {
        if (item.id === action.payload.id) {
          return {
            ...item,
            name: action.payload.name,
          }
        } else {
          return item
        }
      })
    case 'todo/delDone':
      return []
    default:
      return state
  }
}
export default todo
